package servlet;

import modle.User;
import org.apache.commons.beanutils.BeanUtils;
import service.UserService;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.util.Map;

@WebServlet(name = "UserChangeAddressServlet", urlPatterns = "/user_changeaddress")
public class UserChangeAddressServlet extends HttpServlet {
    private UserService userService = new UserService();
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        doPost(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {


        User user = (User) request.getSession().getAttribute("user");
        Map<String, String[]> parameterMap = request.getParameterMap();
        try {
            BeanUtils.copyProperties(user,parameterMap);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }

        userService.updateUserAddress(user);

        request.setAttribute("msg","收件信息更新成功!");

        request.getRequestDispatcher("/user_center.jsp").forward(request,response);

    }
}
